package picture;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CleanPic2 {


    public static void main(String[] args) throws IOException {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入压缩壁纸目录：");
        String compressPath = sc.nextLine();
        System.out.println("请输入壁纸目录");
        String picturePath = sc.nextLine();
        System.out.println("请输入输出目录");
        String outPath = sc.nextLine();

        File compressOut = new File(outPath, "待删除的壁纸（压缩版）");
        if (!compressOut.exists()) {
            compressOut.mkdir();
        }
        File pictureOut = new File(outPath, "待压缩的壁纸");
        if (!pictureOut.exists()) {
            pictureOut.mkdir();
        }


        Map<String, File> pictureMap = new HashMap<>();
        Set<String> compressSet = new HashSet<>();


        System.out.println("Start。。。");
        File pictureDir = new File(picturePath);


        // 处理壁纸
        for (File pic : pictureDir.listFiles()) {
            String fileName = pic.getName();
            if (fileName.equals("desktop.ini")) {
                pic.delete();
                System.out.println(fileName + " is deleted");
                continue;
            }
            int a = fileName.lastIndexOf('.');
            // remove 【4K】 information
            pictureMap.put(fileName.substring(0, a - 4), pic);
        }

        // 清除压缩图片
        File compressDir = new File(compressPath);
        for (File pic : compressDir.listFiles()) {
            String fileName = pic.getName();
            if (fileName.equals("desktop.ini")) {
                pic.delete();
                System.out.println(fileName + " is deleted");
                continue;
            }
            int a = fileName.lastIndexOf('.');
            String name = fileName.substring(0, a);
            if (!pictureMap.containsKey(name)) {
                pic.renameTo(new File(compressOut, fileName));
                System.out.println(name + " 被删除到 待删除的壁纸（压缩版） 文件夹");
            } else {
                compressSet.add(name);
            }
        }

        // 添加待压缩壁纸
        for (Map.Entry<String, File> fileEntry : pictureMap.entrySet()) {
            if (fileEntry.getValue() != null && !compressSet.contains(fileEntry.getKey())) {
                FileChannel source = new FileInputStream(fileEntry.getValue()).getChannel();
                FileChannel destination = new FileOutputStream(new File(pictureOut, fileEntry.getValue().getName())).getChannel();
                source.transferTo(0, source.size(), destination);
                source.close();
                destination.close();
                System.out.println(fileEntry.getKey() + " 被复制到 待压缩的壁纸 文件夹");
            }
        }




        System.out.println();
        System.out.println("--------------------------------------------");
        System.out.println("END!");
    }


}
